package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.Book;
import com.web.entity.ResponseResult;
import com.web.service.BookService;
import com.web.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lenovo
 * @Description
 * @Date2025-3-13 20:17
 */
@WebServlet("/findBook")
public class FindBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        BookService bookService = new BookServiceImpl();
        List<Book> allBook = bookService.findAllBooks();

        // 返回 JSON 数据
        ResponseResult responseResult = new ResponseResult(200, "ok", allBook);

        // 将 ResponseResult 转换为 JSON 并写入响应
        resp.getWriter().write(JSON.toJSONString(responseResult));
    }
}
